package com.example.black.myapplication.uiflow;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2019/01/17/15:21
 *     desc   : 流式标签布局
 *     version: 1.0
 * </pre>
 */

public class FlowTagLayout extends ViewGroup {

    private static final int OFFSET = 80;

    public FlowTagLayout(Context context) {
        super(context);
    }

    public FlowTagLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlowTagLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = 0;
        int right = 0;
        int top = 0;
        int bottom = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++ ){
            View child = getChildAt(i);
            left = i * OFFSET;
            right = left + child.getMeasuredWidth();
            bottom = top + child.getMeasuredHeight();
            child.layout(left,top,right,bottom);
            top += child.getMeasuredHeight();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        //1.测量自己的尺寸
        int childCount = getChildCount();
        for (int i = 0; i < childCount;i++){
            View child = getChildAt(i);
            LayoutParams lp = child.getLayoutParams();
            int childWidthSpec = getChildMeasureSpec(widthMeasureSpec,0,lp.width);
            int childHeightSpec = getChildMeasureSpec(heightMeasureSpec,0,lp.height);
            //1.2 将上面测量后的结果，传给每一个子View，子view测量自己的尺寸
            child.measure(childWidthSpec,childHeightSpec);
        }
        int width = 0;
        int height = 0;
        switch (widthMode){
            case MeasureSpec.EXACTLY:
                width = widthSize;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.UNSPECIFIED:
                for (int i = 0; i < childCount; i++){
                    View child = getChildAt(i);
                    int widthAndOffset = i * OFFSET + child.getMeasuredWidth();
                    width = Math.max(widthSize,widthAndOffset);
                }
                break;
        }

        switch (heightMode){
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
            case MeasureSpec.UNSPECIFIED:
                for (int i = 0; i < childCount; i++){
                    View child = getChildAt(i);
                    height = height + child.getMeasuredHeight();
                }
                break;
        }
        setMeasuredDimension(width,height);
    }
}
